package com.jrmf.redenvelopes.order.job;

import com.jrmf.redenvelopes.order.common.ApplicationContextProvider;
import com.jrmf.redenvelopes.order.dao.DepositdataMfkjImportlogMapper;
import com.jrmf.redenvelopes.order.dao.JdpaySignPayMapper;
import com.jrmf.redenvelopes.order.dbRouter.DynamicDataSourceContextHolder;
import com.jrmf.redenvelopes.order.model.DepositdataMfkjImportlog;
import com.jrmf.redenvelopes.order.model.JdpaySignPay;
import com.jrmf.redenvelopes.order.util.DateUtil;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.*;

public class DepositDataETLJob implements Job {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    //实现Job接口并且在execute方法中实现自己的业务逻辑
    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        long start =System.currentTimeMillis();
        logger.info("入金数据抽取任务开始...");

        JdpaySignPayMapper jdpaySignPayMapper = ApplicationContextProvider.getBean("jdpaySignPayMapper");
        DepositdataMfkjImportlogMapper depositdataMfkjImportlogMapper = ApplicationContextProvider.getBean("depositdataMfkjImportlogMapper");

        Queue<String> q =new LinkedList<>();
        q.add("cj");
        q.add("dlh");
        q.add("xiangliao");
        q.add("yibao");
        q.add("mow");
        q.add("chuiniu");
        q.add("saikun");
        q.add("zwl");
        q.add("xianliao");
        q.add("gyy");
        q.add("xiangliaob");
        String day = DateUtil.dateToStr(DateUtil.getYesterday(),"yyyyMMdd");

        int successCount =0;//执行成功的渠道数
        int failCount =0;//执行失败的渠道数
        List<String> failList =new ArrayList<>();//执行失败的渠道
        List<String> emptyList =new ArrayList<>();//数据为空的渠道

        while (!q.isEmpty()){
            String dataSource =q.poll();
            DynamicDataSourceContextHolder.setDataSourceName(dataSource);
            //读数据-查询单天总量
            Map<String,Object> map =new HashMap<>();
            map.put("dateTime",day);
            List<JdpaySignPay> list=jdpaySignPayMapper.selectJds(map);
            int sourceCount  =list.size();
            logger.info("数据源"+dataSource+"的数据量为"+sourceCount);

            if(sourceCount>0){ //有待抽取数据
                DynamicDataSourceContextHolder.setDataSourceName("dataSource");
                if (sourceCount>1024){
                    //分批次插入
                    int pageSize =0;
                    if(sourceCount%1024==0){
                        pageSize=sourceCount/2014;
                    }
                    else{
                        pageSize=(sourceCount/1024)+1;
                    }
                    int subSuccessCount =0;
                    for(int i=1;i<=pageSize;i++){
                        //创建新的
                        if(i==pageSize){
                            //从 (i-1)*1024--totalNum
                            List<JdpaySignPay> subList= list.subList((i-1)*1024,sourceCount);
                            //System.out.println(subList);
                            int r =jdpaySignPayMapper.batchInsertJds(subList);
                            if (r==(sourceCount-(i-1)*1024)){
                                subSuccessCount=subSuccessCount+r;
                            }
                            else {
                                break;
                            }

                        }
                        else{
                            //从(i-1)*1024--i*1024
                            List<JdpaySignPay> subList= list.subList((i-1)*1024,1024*(i));
                            int r=jdpaySignPayMapper.batchInsertJds(subList);
                            if (r==1024){
                                subSuccessCount =subSuccessCount+r;
                            }
                            else{
                                break;
                            }
                        }
                    }
                    if (subSuccessCount ==sourceCount){
                        successCount=successCount+1;
                    }
                    else{
                        failCount =failCount+1;
                        failList.add(dataSource);
                    }
                }
                else{
                    //单批次直接插入
                    int result =jdpaySignPayMapper.batchInsertJds(list);
                    if (result ==sourceCount){//判断是否插入无误
                        successCount=successCount+1;
                    }
                    else{
                        failCount =failCount+1;
                        failList.add(dataSource);
                    }
                }
            }
            else{//没有
                emptyList.add(dataSource);
                successCount=successCount+1;
            }
        }
        logger.info("成功渠道数:"+successCount);
        logger.info("空渠道："+emptyList.toString());
        logger.info("失败渠道数:"+failCount);
        logger.info("失败渠道:"+failList.toString());

        try {
            DepositdataMfkjImportlog depositdataMfkjImportlog =new DepositdataMfkjImportlog();
            depositdataMfkjImportlog.setCreatedTime(DateUtil.dateToStr(new Date(),"yyyy-MM-dd HH:mm"));
            depositdataMfkjImportlog.setEmptyList(emptyList.toString());
            depositdataMfkjImportlog.setFailList(failList.toString());
            depositdataMfkjImportlog.setSourceTable("jdpay_sign_pay");
            depositdataMfkjImportlog.setSuccessCount(successCount);
            depositdataMfkjImportlog.setDayBiz(day);

            int rd =depositdataMfkjImportlogMapper.insert(depositdataMfkjImportlog);
            if (rd>0){
                logger.info("魔方入金数据持久化log成功存储!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.info("入金数据抽取任务结束...");
        long end =System.currentTimeMillis();
        logger.info("任务执行共计"+(end-start)/1000+"s");
    }

}
